<?php


namespace api\channel\jutuike;

use think\Exception;
use think\facade\Log;
use think\helper\Arr;

class FoodUnion extends Union
{

    public $scene = 'kfc';//默认肯德基

    const PRODUCTS = [
        [
            'product_type' => 'kfc',
            'name' => '肯德基',
            'jump_url' =>'/tq_kfc/act',
            'type' =>'union_food',
            'image_url' => 'https://s1.ax1x.com/2022/05/07/OMKV0J.png'
        ],
        [
            'product_type' => 'mcdonald',
            'name' => '麦当劳',
            'jump_url' => '/mcdonald/act',
            'type' =>'union_food',
            'image_url' => 'https://s1.ax1x.com/2022/05/07/OMKkXF.png'
        ],

        [
            'product_type' => 'pagoda',
            'name' => '百果园',
            'jump_url' => '/pagoda/act',
            'type' =>'union_food',
            'image_url' => 'https://s1.ax1x.com/2022/05/07/OMKmkR.png'
        ],
        [
            'product_type' => 'spk',
            'name' => '星巴克',
            'jump_url' => '/spk/act',
            'type' =>'union_food',
            'image_url' => 'https://s1.ax1x.com/2022/05/07/OMKZ79.png'
        ],
        [
            'product_type' => 'heytea',
            'name' => '喜茶',
            'jump_url' => '/heytea/act',
            'type' =>'union_food',
            'image_url' => 'https://s1.ax1x.com/2022/05/07/OMKilT.png'
        ],

        [
            'product_type' => 'burgerking',
            'name' => '汉堡王',
            'jump_url' => '/burgerking/act',
            'type' =>'union_food',
            'image_url' => 'https://s1.ax1x.com/2022/05/07/OMKnt1.png'
        ],
        [
            'product_type' => 'nayuki',
            'name' => '奈雪的茶',
            'jump_url' => '/nayuki/act',
            'type' =>'union_food',
            'image_url' => 'https://s1.ax1x.com/2022/05/07/OMKEm4.png'
        ],

        [
            'product_type' => 'luckin',
            'name' => '瑞幸咖啡',
            'jump_url' => '/luckin/act',
            'type' =>'union_food',
            'image_url' => 'https://s1.ax1x.com/2022/05/07/OMKF6U.png'
        ],




    ];

    public function __construct($scene = 'kfc')
    {
        $this->scene = $scene;

        parent::__construct(PRODUCT_FOOD_SHOP_SERV); //连锁餐饮
    }



    /**
     * 产品转化链接
     */
    public function productUrl($args,$act_id=0){

        $sid = $this->getSid($args['agent_id'], $args['user_id'],$args['channel_id']??'');
        $mobile = $args['mobile']??'';
        $productInfo = Arr::where(static::PRODUCTS,function ($val){
                if ($this->scene == $val['product_type']){
                    return true;
                }
            }) ??[];
        if (empty($productInfo)){
            throw new Exception('参数错误');
        }


        $productInfo = current($productInfo);

        $url = $this->baseUrl . $productInfo['jump_url'];

        $longitude = input('longitude','');
        $latitude = input('latitude','');

        Log::info('获取经纬度：'.$longitude);


        $defaults = [
            'sid' => $sid,
            'type' => 1,
            'link_type' => 'h5',
            'mobile' => $mobile,
            'longitude' => $longitude,
            'latitude' => $latitude,
        ];

        $data = [
            'apikey' => $this->apikey,
        ];
        $data = handleParameters($data,$defaults,$args);

        $res = curlPost($url, $data);
        $res = json_decode($res, true);
        if ($res['data']) {
            $res['data']['product_name'] = $productInfo['name'];
        }
        return $res['data']??[];
    }


}